package com.kwzjx.product.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.kwzjx.common.core.utils.DateUtils;
import com.kwzjx.common.security.utils.SecurityUtils;
import com.kwzjx.product.domain.MallProductRuleAttrInfo;
import com.kwzjx.product.domain.model.MallProductAttrIfoModel;
import com.kwzjx.product.service.IMallProductRuleAttrInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kwzjx.product.mapper.MallProductRuleInfoMapper;
import com.kwzjx.product.domain.MallProductRuleInfo;
import com.kwzjx.product.service.IMallProductRuleInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品规格Service业务层处理
 *
 * @author kwzjx
 * @date 2024-05-28
 */
@Service
public class MallProductRuleInfoServiceImpl implements IMallProductRuleInfoService
{
    @Autowired
    private MallProductRuleInfoMapper mallProductRuleInfoMapper;

    @Autowired
    private IMallProductRuleAttrInfoService iMallProductRuleAttrInfoService;

    /**
     * 查询商品规格
     *
     * @param id 商品规格主键
     * @return 商品规格
     */
    @Override
    public MallProductRuleInfo selectMallProductRuleInfoById(Long id)
    {
        return mallProductRuleInfoMapper.selectMallProductRuleInfoById(id);
    }

    /**
     * 查询商品规格列表
     *
     * @param mallProductRuleInfo 商品规格
     * @return 商品规格
     */
    @Override
    public List<MallProductRuleInfo> selectMallProductRuleInfoList(MallProductRuleInfo mallProductRuleInfo)
    {
        return mallProductRuleInfoMapper.selectMallProductRuleInfoList(mallProductRuleInfo);
    }

    /**
     * 新增商品规格
     *
     * @param mallProductRuleInfo 商品规格
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMallProductRuleInfo(MallProductRuleInfo mallProductRuleInfo)
    {

        Long userid = SecurityUtils.getLoginUser().getUserid();
        mallProductRuleInfo.setCreateBy(String.valueOf(userid));
        Long loginUserId = SecurityUtils.getLoginUser().getUserid();
        mallProductRuleInfo.setUpdateBy(String.valueOf(loginUserId));


        mallProductRuleInfo.setCreateTime(DateUtils.getNowDate());
        mallProductRuleInfo.setUpdateTime(DateUtils.getNowDate());
        int i = mallProductRuleInfoMapper.insertMallProductRuleInfo(mallProductRuleInfo);
        if(i>0){
            Long ruleId = mallProductRuleInfo.getId();
            String ruleAttr = mallProductRuleInfo.getRuleAttr();
            List<MallProductRuleAttrInfo> ruleAttrInfoList = new ArrayList<>();
            List<MallProductAttrIfoModel> mallProductRuleAttrInfoModels = JSON.parseArray(ruleAttr, MallProductAttrIfoModel.class);
            if(CollectionUtils.isNotEmpty(mallProductRuleAttrInfoModels)){
                for (MallProductAttrIfoModel mallProductRuleAttrInfoModel : mallProductRuleAttrInfoModels) {
                    String ruleType = mallProductRuleAttrInfoModel.getRuleType();
                    List<String> ruleAttrList = mallProductRuleAttrInfoModel.getRuleAttrList();
                    for (String ruleAttrString : ruleAttrList) {
                        MallProductRuleAttrInfo mallProductRuleAttrInfo = new MallProductRuleAttrInfo();
                        mallProductRuleAttrInfo.setAttrValue(ruleAttrString);
                        mallProductRuleAttrInfo.setName(ruleType);
                        mallProductRuleAttrInfo.setRuleId(ruleId);
                        ruleAttrInfoList.add(mallProductRuleAttrInfo);
                    }
                }

                iMallProductRuleAttrInfoService.deleteMallProductRuleAttrInfoByRuleId(ruleId);
                for (MallProductRuleAttrInfo mallProductRuleAttrInfo : ruleAttrInfoList) {
                    iMallProductRuleAttrInfoService.insertMallProductRuleAttrInfoList(mallProductRuleAttrInfo);
                }
            }




        }
        return i;
    }

    /**
     * 修改商品规格
     *
     * @param mallProductRuleInfo 商品规格
     * @return 结果
     */
    @Override
    public int updateMallProductRuleInfo(MallProductRuleInfo mallProductRuleInfo)
    {
        mallProductRuleInfo.setUpdateTime(DateUtils.getNowDate());
        return mallProductRuleInfoMapper.updateMallProductRuleInfo(mallProductRuleInfo);
    }

    /**
     * 批量删除商品规格
     *
     * @param ids 需要删除的商品规格主键
     * @return 结果
     */
    @Override
    public int deleteMallProductRuleInfoByIds(Long[] ids)
    {
        return mallProductRuleInfoMapper.deleteMallProductRuleInfoByIds(ids);
    }

    /**
     * 删除商品规格信息
     *
     * @param id 商品规格主键
     * @return 结果
     */
    @Override
    public int deleteMallProductRuleInfoById(Long id)
    {
        return mallProductRuleInfoMapper.deleteMallProductRuleInfoById(id);
    }
}
